﻿/**********************************************************
 * 模    块	：	ListControl控件扩展类。
 * 作    者	：	Jasper
 * 编写时间	：	2010-04-24
 * 实现功能	：	ListControl控件进行方法扩展
 * 详细描述	：	对ListControl控件进行扩展
 * *********************************************************/

namespace Basic4Net
{
	using System;
	using System.Web.UI.WebControls;
	using System.Collections;

	public static class ListControlExtensions
	{
		#region ListControl扩展
		/// <summary>
		/// 根据值选中项。
		/// </summary>
		/// <param name="list">控件</param>
		/// <param name="value">值</param>
		public static void _SelectedByValue(this ListControl list, object value)
		{
			if (value == DBNull.Value || value == null)
			{
				list.SelectedIndex = -1;
			}
			else
			{
				list.SelectedIndex = list.Items.IndexOf(list.Items.FindByValue(value.ToString()));
			}
		}

		/// <summary>
		/// 根据文本选中项
		/// </summary>
		/// <param name="list">控件</param>
		/// <param name="text">文本</param>
		public static void _SelectedByText(this ListControl list, string text)
		{
			list.SelectedIndex = -1;
			list.SelectedIndex = list.Items.IndexOf(list.Items.FindByText(text.ToString()));
		}

		/// <summary>
		/// 获取列表控件被多项选择的文本和值，文本格式为“a,b,c,d”，值格式为“,1,2,3,4,”
		/// </summary>
		/// <param name="list">控件</param>
		/// <param name="texts">文本串</param>
		/// <param name="values">值串</param>
		public static void _GetSelected(this ListControl list, out string texts, out string values)
		{
			string _texts = "";
			string _values = ",";
			//foreach (ListItem item in list.Items)
			//{
			//    if (item.Selected)
			//    {
			//        texts += item.Text + ",";
			//        values += item.Value + ",";
			//    }
			//}
			System.Threading.Tasks.Parallel.For(0, list.Items.Count, (i) =>
			{
				if (list.Items[i].Selected)
				{
					_texts += list.Items[i].Text + ",";
					_values += list.Items[i].Value + ",";
				}
			});
			if (_texts.Length > 0)
			{
				_texts = _texts.TrimEnd(',');
			}
			texts = _texts;
			values = _values;
		}

		/// <summary>
		/// 根据值串，选定多个项目
		/// </summary>
		/// <param name="list">控件</param>
		/// <param name="values">值串</param>
		public static void _SelectedByValues(this ListControl list, string values)
		{
			if (!String.IsNullOrEmpty(values))
			{
				list.SelectedIndex = -1;
				values = "," + values + ",";
				string strSelected;
				foreach (ListItem item in list.Items)
				{
					strSelected = "," + item.Value + ",";
					if (values.IndexOf(strSelected) > -1)
					{
						item.Selected = true;
						values.Replace(strSelected, ",");
						if (values == ",")
							break;
					}
				}
			}
		}

		/// <summary>
		/// 根据值串，选定多个项目
		/// </summary>
		/// <param name="list">控件</param>
		/// <param name="values">值串</param>
		/// <param name="splitFlag">分隔符</param>
		public static void _SelectedByValues(this ListControl list, string values, string splitFlag)
		{
			if (!String.IsNullOrEmpty(values))
			{
				list.SelectedIndex = -1;
				values = splitFlag + values + splitFlag;
				string strSelected;
				foreach (ListItem item in list.Items)
				{
					strSelected = splitFlag + item.Value + splitFlag;
					if (values.IndexOf(strSelected) > -1)
					{
						item.Selected = true;
						values.Replace(strSelected, splitFlag);
						if (values == splitFlag)
							break;
					}
				}
			}
		}

		/// <summary>
		/// 绑定List控件数据源
		/// </summary>
		/// <param name="list">控件</param>
		/// <param name="dataSource">数据源</param>
		/// <param name="textField">文本字段名</param>
		/// <param name="valueField">值字段名</param>
		public static void _DataBind(this ListControl list, IList dataSource, string textField, string valueField)
		{
			list.DataSource = dataSource;
			list.DataTextField = textField;
			list.DataValueField = valueField;
			list.DataBind();
		}
		#endregion
	}
}
